package com.smartrobot.smartrobot.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.pagerHolder.BaseFragment;
import com.smartrobot.smartrobot.pagerHolder.InformationFragment;
import com.smartrobot.smartrobot.pagerHolder.MessageFragment;
import com.smartrobot.smartrobot.pagerHolder.PersonalFragment;
import com.smartrobot.smartrobot.pagerHolder.SocialCircleFragment;

import java.util.ArrayList;
import java.util.List;


public class Main extends FragmentActivity{

    List<Fragment> fragmentList = new ArrayList<Fragment>();
    ViewPager viewPager;

    private InformationFragment informationFragment;
    private MessageFragment messageFragment;
    private SocialCircleFragment socialCircleFragment;
    private PersonalFragment personalFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_main);

        viewPager = (ViewPager)findViewById(R.id.main_viewpager);

        informationFragment = new InformationFragment();
        messageFragment = new MessageFragment();
        socialCircleFragment = new SocialCircleFragment();
        personalFragment = new PersonalFragment();

        fragmentList.add(informationFragment);
        fragmentList.add(messageFragment);
        fragmentList.add(socialCircleFragment);
        fragmentList.add(personalFragment);



        viewPager.setOffscreenPageLimit(4);
        viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), fragmentList));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {

                switch (i){

                    case 0:
                        informationFragment.init();
                        break;
                    case 1:
                        messageFragment.init();
                        break;
                    case 2:
                        socialCircleFragment.init();
                        break;
                    case 3:
                        personalFragment.init();
                        break;

                }

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });


    }

    class FragmentAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragmentList;

        public FragmentAdapter(FragmentManager fm,List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int i) {

            Log.d("==========>","选择页面"+i);

            return (fragmentList == null || fragmentList.size() == 0) ? null : fragmentList.get(i);
        }

        @Override
        public int getCount() {
            return fragmentList == null ? 0 : fragmentList.size();
        }



    }



}
